<template>
	<view class="mask" @tap="cancel" @touchmove.stop>
		<view class="mask-content" @tap.stop>
			<text class="new-iconfont icon-close close-btn" @tap.stop="cancel"></text>
			<view class="title">选择要兑换的商品</view>
			<view class="sku-list">
				<view class="item" :class="{selected: selectedSku.id === item.id}" @tap="selectSku(item)" v-for="(item, index) in task.skus">
					<image :src="item.thumb" mode="widthFix" class="thumb"></image>
					<view class="right">
						<view class="title">{{item.title}}</view>
						<view class="stock">剩余{{item.stock}}份</view>
					</view>
				</view>
			</view>

			<view class="btn-c">
				<button class="btn cancel bg-cancel" @tap="cancel">再想想</button>
				<button class="btn submit bg-yellow" @tap="submit">确认兑换</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				selectedSku: {}
			}
		},
		props: {
			task: {
				type: Object
			}
		},
		computed: {},
		watch: {},
		onLoad(e) {

		},
		created() {
			if (this.task.skus.length === 1) {
				this.selectedSku = this.task.skus[0]
			}
		},
		methods: {
			st_loadData(){
				let st = true;
				if(st == 1){
					console.info(1)
				}
			},
			st_list(){
				let status = true;
				if(status){
					status = false;
					console.info(222)
				}
			},
			st_pay(){
				let s = 1;
				if(s == 1){
			
				}else if(s == 2){
				
				}
			},
			selectSku (sku) {
				if (sku.stock <= 0) {
					uni.showToast({
						title: '此款已兑光了哦~',
						icon: 'none'
					})
					return false 
				}
				this.selectedSku = sku
			},
			cancel() {
				this.$emit('close')
			},
			submit() {
				uni.showLoading()
				if (!this.selectedSku.id) {
					uni.showToast({
						title: '请选择要兑换的商品',
						icon: 'none'
					})
					return false
				}
				
 				this.$http(`/jika/swap/confirm`, 'post', {
					sku_id: this.selectedSku.id
				}).then(res => {
					uni.hideLoading()
					this.$emit('success')
					uni.showToast({
						title: '恭喜兑换成功，即将跳转~',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/myBox/index'
						})
					}, 1500)
				})
			},
			toPage(url) {
				uni.navigateTo({
					url: url
				})
			}
		},
		onPageScroll(e) {}
	}
</script>

<style lang="scss" scoped>
	
	.st_main{
		background-color: transparent;
		font-size: 28rpx;
		font-weight: bold;
		display: flex;
		flex-direction: row;
	}
	.st_list{
		background-color: transparent;
		display: flex;
		font-size: 28rpx;
	}
	
	.small-text {
		// font-size: 80%;
	}

	.mask {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.mask-content {
		width: 660rpx;
		// height: 680rpx;
		background-color: #EDEDED;
		background-color: white;
		border-radius: 20rpx;
		// display: flex;
		// flex-direction: column;
		// align-items: center;
		// animation: show 0.8s;
		padding: 60rpx 60rpx 50rpx 60rpx;
		box-sizing: border-box;
		position: relative;

		.close-btn {
			position: absolute;
			right: 26rpx;
			top: 26rpx;

			color: #999;
		}

		.title {
			text-align: center;
			font-size: 36rpx;
			font-weight: 500;
		}

		.sku-list {
			margin-top: 50rpx;
			.item {
				display: flex;
				align-items: center;
				padding: 20rpx;
				// border-bottom: 1rpx solid #eee;
				border: 2rpx solid rgba(0, 0, 0, 0);
				border-bottom: 1rpx solid #eee;
				border-radius: 6rpx;
				
				&:first-child {
					border-top: 1rpx solid #eee;
				}
				
				&.selected {
					border: 2rpx solid #F7AF0A;
				}
				
				.thumb {
					width: 100rpx;
					height: 100rpx;
					flex: 0 0 100rpx;
					background: #FFFFFF;
					border: 6rpx solid #FFF0D7;
					border-radius: 20rpx;
				}
				
				.right {
					margin-left: 10rpx;
					.title {
						font-size: 30rpx;
						font-family: PingFang;
						font-weight: bold;
						color: #333333;
						
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}
					.stock {
						font-size: 26rpx;
						font-family: PingFang;
						font-weight: 500;
						color: #B5AB99;
					}
				}
			}
		}


		.btn-c {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.btn {
				padding: 0;
				margin: 50rpx auto 0rpx auto;
				width: 46%;
				border-radius: 20rpx;
				height: 86rpx;
				line-height: 86rpx;
				text-align: center;
				font-size: 30rpx;
				font-weight: bold;
				background-color: #ffcc00;
				// box-shadow: 0 0 20rpx rgba(255, 220, 0, 0.6);
				color: white;

				&:after {
					border: none;
				}

				&.submit {}
			}
		}
	}
</style>
